<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");

class IndexController extends Controller {

    private $user_id;

    public function _initialize()
    {
        if(!check_login()){
            $this->error("登录异常,请先登录","/Home/Login/index");
        }
        $this->user_id  =   session('user_id');
        $cart_num   =   M("user_category")->where(array("user_id"=>$this->user_id))->count()+0;
        $this->assign("cart_num",$cart_num);
    }

    //系统首页
    public function index(){
        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
        if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }

        $lists  =   M("users")->where(array("class_id"=>$info['class_id']))->order("student_id asc")->select();

        $this->assign("info",$info);
        $this->assign("lists",$lists);
        $this->display();
    }

    public function userInfo() {
        $id     =   I("get.id");
//        $info   =   M("users")->where(array("id"=>$id))->find();
        $info  =   M('users u')
            ->join("ab_department as d ON d.id = u.dep_id","left")
            ->join("ab_class as c ON c.id = u.class_id","left")
            ->where(array("u.id"=>$id))
            ->field("u.id,u.student_id,u.pic,u.sex,u.name,u.phone,u.age,u.phone,u.email,u.qq,u.constellation,u.autograph,u.address,u.work_address,d.name depName,c.name className")
            ->find();
        $this->assign("info",$info);
        $this->display();
    }

    public function personal() {
//        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
        $info  =   M('users u')
            ->join("ab_department as d ON d.id = u.dep_id","left")
            ->join("ab_class as c ON c.id = u.class_id","left")
            ->where(array("u.id"=>$this->user_id))
            ->field("u.id,u.student_id,u.pic,u.sex,u.name,u.phone,u.age,u.phone,u.email,u.qq,u.constellation,u.autograph,u.address,u.work_address,d.name depName,c.name className")
            ->find();

        if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }
        $this->assign("info",$info);
        $this->display();
    }

    public function information() {
        if($_POST){

            $file   =   $_FILES['pic'];
            $dir='./Uploads/users/';
            $data   =   I("post.");
            if(!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            if(!checkEmail($data['email']) && $data['email'])
                $this->error("邮箱格式错误");
            if($file['name']){
                $getPic =   upPic($file,$dir);
                if($getPic['tag']   ==  '1'){
                    $this->error($getPic['msg']);
                }else{
                    $data['pic']    =   $getPic;
                    $oPic           =   M("users")->where(array("id"=>$this->user_id))->getField("pic");
                }
            }
            $res    =   M("users")->where(array("id"=>$this->user_id))->save($data);
            if($res     !== false){
                if($oPic)   unlink('.'.$oPic);
                $this->redirect("/Home/Index/information");
            }else $this->error("编辑失败");
        }else{
            $info   =   M("users")->where(array("id"=>$this->user_id))->find();
            $department   =   M("department")->select();

            if($info['dep_id']){
                $classes = M("class")->where(array("dep_id"=>$info['dep_id']))->select();
                $this->assign("classes",$classes);
            }

            $this->assign("info",$info);
            $this->assign("department",$department);
            $this->display();
        }
    }

    public function depClass () {
        $dep_id       =   I('get.depId');
        $data   =   M("class")->where(array("dep_id"=>$dep_id))->select();
        echo json_encode($data);
    }


}